package com.caigou.service.impl;

import com.caigou.bean.cms.entity.ArticleAudio;
import com.caigou.mapper.cms.ArticleAudioMapper;
import com.caigou.pojo.ArticleAudioData;
import com.caigou.service.ArticleAudioService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service("articleAudioService")
public class ArticleAudioServiceImpl implements ArticleAudioService {

    @Resource
    private ArticleAudioMapper articleAudioMapper;

    @Override
    public ArticleAudio saveArticleAudioData(String articleId, Integer audioIssue, Integer audioInfoStatus, String audioInfo,
                                             Integer audioId, String audioName, String audioUrl, Integer audioTime, String audioContent) {
        ArticleAudio articleAudio = new ArticleAudio();
        articleAudio.setArticleId(articleId);
        articleAudio.setAudioIssue(audioIssue);
        articleAudio.setAudioInfoStatus(audioInfoStatus);
        articleAudio.setAudioInfo(audioInfo);
        articleAudio.setAudioContent(audioContent);
        articleAudio.setAudioId(audioId);
        articleAudio.setAudioName(audioName);
        articleAudio.setAudioUrl(audioUrl);
        articleAudio.setAudioTime(audioTime);
        articleAudioMapper.saveArticleAudioData(articleAudio);
        return articleAudio;
    }

    @Override
    public ArticleAudioData loadArticleAudioData(String articleId) {
        return articleAudioMapper.loadArticleAudioData(articleId);
    }

    @Override
    public int updateArticleAudioData(String articleId, Integer audioIssue, Integer audioInfoStatus, String audioInfo,
                                      Integer audioId, String audioName, String audioUrl, Integer audioTime, String audioContent) {
        ArticleAudio articleAudio = articleAudioMapper.loadArticleAudio(articleId);
        boolean isCreated = false;
        if (null == articleAudio){
            isCreated = true;
            articleAudio = new ArticleAudio();
        }
        articleAudio.setArticleId(articleId);
        articleAudio.setAudioIssue(audioIssue);
        articleAudio.setAudioInfoStatus(audioInfoStatus);
        articleAudio.setAudioInfo(audioInfo);
        articleAudio.setAudioContent(audioContent);
        articleAudio.setAudioId(audioId);
        articleAudio.setAudioName(audioName);
        articleAudio.setAudioUrl(audioUrl);
        articleAudio.setAudioTime(audioTime);
        if (isCreated){
            return articleAudioMapper.saveArticleAudioData(articleAudio);
        } else {
            return articleAudioMapper.updateArticleAudioData(articleAudio);
        }
    }


    public int deleteArticleAudioByArticleId(String articleId){
        return articleAudioMapper.deleteArticleAudioByArticleId(articleId);
    }
}
